import React, { Component } from 'react';
//框架样式和公共样式
import './less/common/common.less';
import './less/common/icon.less';
//router
import { Route,Switch,withRouter} from 'react-router-dom';
import { hot } from 'react-hot-loader';

//需要用到的比对配置(layout/menu/breadcrumb)
import routes from './router';
window.routeConfig = routes;

//公共方法
import './utils/common';
import './utils/config';
import './utils/ajax';

//mock(模拟后台数据)
import '../mock/index';

class App extends Component {
    isValidLogin = (path)=>{
        const { history } = this.props;
        if(com.getData('user','uid')){
            {/*不能访问的权限对应的url (如果当前链接是则跳转无权限页面)*/}
            let authority = com.getData('user','authority');
            if(authority[path]){
                history.replace('/app/403');
            }

            {/*如果链接是登陆则自动跳转首页*/}
            if(path == '/login' || path == '/'){
                history.replace('/app/nav1/first');
            }
        }else{
            history.replace('/login');
        }
    };
    componentWillMount(){
        this.isValidLogin(this.props.location.pathname)
    }
    componentWillReceiveProps(nextProps) {
        if (nextProps.location.pathname != this.props.location.pathname) {
            this.isValidLogin(nextProps.location.pathname)
        }
    }
    render() {
        return (
            <Switch>
                {
                    routes.map((item, key) => {
                        return (
                            <Route
                                path={item.path}
                                render={props => <item.component {...props} routes={item.routes} />}
                                exact={!!item.exact}
                                key={item.path + key}
                            />
                        )
                    })
                }
            </Switch>
        );
    }
}
export default hot(module)(withRouter(App))
//export default withRouter(App);
/*
<Route exact path="/" component={Header}/>
<Route path="/state/:id" component={Content}/>
<Route path="/props/:id" component={Parent}/>
<Route exact path="/" render={()=><Redirect to="/app/nav1/first"/>}/>
<Route path='/app' component={Layout} routes={routes} />
<Route path='/login' component={Login}/>
*/
